package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

/**
 *  GansudailyNewsAnalyse  每日甘肃网新闻解析类
 *  xiaonie 2012-11-23
 */
public class GansudailyNewsAnalyse implements AnalyseNews{
	private final String[] regexs={
			"http://[a-z]*[.]gansudaily.com.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]+.shtml",
			"http://jk.gansudaily.com.cn/a/[0-9]{4}/[a-z]*_[a-z]{4}/[0-9]+.html"};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		Integer commnetNum=0;
		Integer clickNum=0;
		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			if(url.startsWith("http://fzzx.gansudaily.com.cn")){
				title=doc.select("h2").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.contents"));
				date=DateUtils.matchDate(doc.select("div[align=center]").text());
			}else if(url.startsWith("http://comment.gansudaily.com.cn")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div[style=padding:0 20px;font-size:14px; color:#000000]"));
				dateStr=doc.select("td.laiy").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://jk.gansudaily.com.cn")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div#article"));
				dateStr=doc.select("h3.show_info").text();
				date=DateUtils.matchDate(dateStr);
				commnetNum=Integer.parseInt(doc.select("span#comment").text());
				clickNum=Integer.parseInt(doc.select("span#hits").text());
				news.setClickNum(clickNum);
				news.setCommentNum(commnetNum);
			}else if(url.startsWith("http://jq.gansudaily.com.cn")){
				title=doc.select("td.zt16_h").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("h1").text();
				}
				content=HtmlCleaner.getContentHtml(url,doc.select("td.F2"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.contentB"));
				}
				dateStr=doc.select("td.zt12_h").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://plrb.gansudaily.com.cn") || url.startsWith("http://lnb.gansudaily.com.cn")){
				title=doc.select("td.F1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.L1"));
				date=DateUtils.matchDate(url);
			}else if(url.startsWith("http://zyrb.gansudaily.com.cn")){
				title=doc.select("div.F1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.L1"));
				date=DateUtils.matchDate(doc.select("div[align=right]").text());
			}else if(url.startsWith("http://wap.gansudaily.com.cn")){
				title=doc.select("title").text();
				title=title.substring(0, title.indexOf("-"));
				content=HtmlCleaner.getContentHtml(url,doc.select("div.lanmu div div"));
				date=DateUtils.matchDate(doc.select("div.lanmu").text());
			}else if(url.startsWith("http://gn.gansudaily.com.cn")){
				title=doc.select("td.zt14_h").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.zt_14"));
				date=DateUtils.matchDate(doc.select("td.zt_12").text());
			}else{
				title=doc.select("h1").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("div.F1").text();
				}
				content=HtmlCleaner.getContentHtml(url,doc.select("div.contentB p"));
				dateStr=doc.select("div.contentB.hui12").text();
				date=DateUtils.matchDate(dateStr);
			}
			if(StringUtils.isBlank(content)){
				content=doc.select("p").html();
			}
			if (date==null){
				date=DateUtils.matchDate(url);
			}
			if(title == null || title.trim().length() == 0 || date == null){
			}

			news.setContent(content);
			news.setTitle(title);
			news.setDate(date);
			news.setType(1);

			String author=doc.select("div.content div.contentB div").text();
			if(author!=null && author.indexOf("稿源：")!=-1){
				author=StringUtils.substringBetween(author, "稿源：", " ").trim();
			}
			news.setAuthor(author);
		}
		return news;
	}

	public static void main(String[] args) {
		GansudailyNewsAnalyse a=new GansudailyNewsAnalyse();
		String url="http://xbsb.gansudaily.com.cn/system/2007/03/11/010283617.shtml";
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
